<%--
  Created by IntelliJ IDEA.
  User: CY
  Date: 2018/8/6
  Time: 11:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>新建报表</title>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" type="text/css" href="../../css/workflow/m_reset.css">
    <link rel="stylesheet" type="text/css" href="../../css/workflow/work/workMonitor.css">
    <link rel="stylesheet" href="../../css/officialDocument/officialDocument.css">
    <link rel="stylesheet" type="text/css" href="../../lib/pagination/style/pagination.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/base.css" />
    <link rel="stylesheet" type="text/css" href="/css/base/base.css?20201106.1" />
    <link rel="stylesheet" href="/lib/layer/skin/default/layer.css">
    <link rel="stylesheet" type="text/css" href="../../css/workflow/work/handle.css"/>
    <script type="text/javascript" src="/js/jquery/jquery-1.9.1.js"></script>
    <script type="text/javascript" src="/js/base/tablePage.js"></script>
    <script src="/lib/layer/layer.js?20201106"></script>
    <script src="/js/base/base.js"></script>
    <script src="../../lib/pagination/js/jquery.pagination.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../js/jquery/jquery.cookie.js"></script>
    <style>
        .container{
            width:100%;
            height:100%;
            margin-top: -30px;
            /*padding: 20px;*/
        }
        .con_left{
            float: left;
            width: 261px;
            height: 100%;
            background: #f5f7f8;
            overflow-y: auto;
        }
        .con_rit{
            width:calc(100% - 261px);
            float: left;
            /*margin-left:20px;*/
        }
        .outerbox{
            width:100%;
            /*border:1px solid #eee;*/
            /*border-radius: 6px;*/
            text-align: center;
            margin: 69px 0 0;
        }
        .outerbox li{
            display: block;
            padding-left: 0px;
            height: 48px;
            line-height: 48px;
            font-size: 16px;
            color: #262f34;

            border-bottom: 1px solid #dcdfe0;
        }
        .outerbox .active{

            background: #c7e1ff;
        }
        .topTit{
            margin: 0px;
            padding: 0px;
            margin-top: 77px;
            margin-right: 10px;
            color: #3f9bca;
            font-size: 14px;
            font-weight: bold;
            margin-bottom: 16px;
            border-bottom: 1px solid #919191;
            height: 40px;
            line-height: 19px;
        }
        .title{
            font-weight: 900;
            color: #262f34;
            font-size: 18px;
            display: inline-block;

            padding:10px 0;
        }

        .sel{
            z-index: 999;
            width: 178px;
            max-height: 250px;
            /*position: absolute;*/
            /*top: 21px;*/
            /*left: 0px;*/
            overflow: auto;
            background: #fff;
            border: 1px solid #e2e3e3;
            display: none;
        }
        .sel li{
            line-height: 24px;
            color: #000;
        }
        #list{
            width:40px;
            height:30px;
            background: #f3f3f3;
            border:1px solid #ccc;
            vertical-align: middle;
            /*position: absolute;*/
            /*right: 0px;*/
            /*top: -8px;*/
            margin: -6px 0px 0px -40px;
            border-radius: 0px 3px 3px 0px;
        }
        #list img{
            margin-top: -2px;
            margin-left: 0px;
        }
        input[name="flowName"],input[name="tableName"],#fenzu{
            width:180px;
            height:30px;
            border-radius: 3px;
            margin-top: -6px;
        }
        .red{
            color: red;
        }
        .tit{
            line-height: 40px;
            font-weight: bold;
            font-size: 14px;
        }
        .con_bom{
            border-top:1px solid #e5e6e7;
            width:100%;
            position: fixed;
            height: 50px;
            text-align: center;
            bottom:0px;
            background: #fff;
        }

        .last,.nexts{

            color: #2a588c;
            /*background-color: #4b8cf7;*/
            font-weight: bold;
            font-size: 14px;
            padding:5px;
            margin-top:10px;
            cursor:pointer;
            width:80px;
            height:30px;
            padding-left: 18px;
        }
        .last{
            background: url('../../img/workflow/work/last.png') no-repeat;
        }
        .nexts{
            background: url('../../img/workflow/work/next.png') no-repeat;
        }
        .save{
            width:80px;
            height:30px;

            color: #2a588c;
            /*background-color: #2f973f;*/
            font-weight: bold;
            font-size: 14px;
            padding:5px 10px;
            cursor:pointer;
            margin-top:10px;
            padding-left: 18px;
            background: url('../../img/workflow/work/saves.png') no-repeat;
        }
        .closes{
            width:80px;
            height:30px;

            color: #2a588c;
            /*background-color: #fa9f1e;*/
            font-weight: bold;
            font-size: 14px;
            padding:5px 10px;
            margin-top:10px;
            padding-left: 18px;
            cursor:pointer;
            background: url('../../img/workflow/work/close.png') no-repeat;
        }
        .con_bom button{
            margin:10px 5px;

        }
        .disabled{
            /*border: 1px solid #dcdcdc;*/
            /*background-color: #ececec;*/
            /*background-image: linear-gradient(to bottom, #eeeeee, #ececec);*/
            /*color: #AAA;*/
            cursor: not-allowed;
        }
        .tabName{
            float:right;
            color: #94918c;
            font-size:15px;
            margin-top: 10px;
            /*display: none;*/
        }
        .msg{
            color: green;
            margin: 0px;
            padding: 0px;
            font-size: 12px;
            line-height: 30px;
        }
        table{
            width:55%;
            min-width:775px;
            border:none;
        }
        table tr{
            border:none;
            height:30px;
        }
        table td{
            line-height: 40px;
            font-weight: bold;
            font-size: 14px;
        }
        table td{
            text-align: left;
        }
        table input{
            width:100px;
            height:28px;
            border-radius: 3px;
        }
        .hidden{
            display: none;
        }
        #addTab,#addTabTwo{
            border: 1px solid #3079ed;
            color: #ffffff;
            background-color: #4b8cf7;
            padding: 4px 12px;
            font-family: Simsun, Arial, sans-serif;
            font-weight: bold;
            font-size: 13px;
            text-align: center;
            cursor: pointer;
        }
        .step{
            display: none;
        }
        .content .show{
            display: block;
        }
        .jin{
            background: #eee;
        }
        .chooseTab,.showName,.jisuan{
            font-weight: normal;
        }
        .edit img,.edits img{
            padding:10px;
        }
        .header{
            height: 38px;
            line-height: 38px;
            background: #3a90f2;
            font-size: 16px;
            font-weight: 500;
            color: #fff;
            padding-left: 15px;
        }
        .content li{
            padding-left:20px;
        }
        .text{
            margin-left:0px;
        }

    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <img src="../../img/workflow/work/newTable.png" alt="">
            <span class="text">新建报表</span>
        </div>
        <div class="con_left">
            <ul class="outerbox">
                <li class="active" dataType="1" style="">设置基本属性</li>
                <li dataType="2">设置查询字段</li>
                <li dataType="3"  style="">设置报表表头</li>
            </ul>
        </div>
        <div class="con_rit">
            <div class="topTit">
                <span class="title">设置基本属性</span>
                <span class="tabName">报表名称：<span id="tabName"></span></span>
            </div>
            <ul class="content">
                <li dataType="1" class="show step">
                    <label style="position: relative;">
                        <div class="tit">所属流程 <b class="red">*</b></div>
                        <input type="text" name="flowName" placeholder="" dataType="" style="padding-left: 5px">
                        <button id="list"><img src="../../img/workflow/work/xiala.png" alt=""></button>
                        <ul class="sel"></ul>
                    </label>
                    <label style="position: relative;">
                        <div class="tit">报表名称 <b class="red">*</b></div>
                        <input type="text" name="tableName"  dataType="" style="padding-left: 5px">
                    </label>
                    <label style="position: relative;">
                        <div class="tit">分组字段 </div>
                        <select name="fenzu" class="chooseTab" id="fenzu">
                            <option value="run_id">不分组</option>
                        </select>
                    </label>
                </li>

                <li dataType="2" class="step">
                    <table style="width: 38%;min-width: 380px;">
                        <thead>
                        <tr>
                            <td style="width: 10%">序号</td>
                            <td >选择表单字段</td>
                            <td>字段显示名称</td>
                        </tr>
                        </thead>
                        <tbody class="lists">
                        <tr>
                            <td class="num">1</td>
                            <td><select name="" class="chooseTab field">
                                <option value=""></option></select></td>
                            <td><input class="showName field" type="text"></td>
                            <td class="edits">
                                <img src="../../img/workflow/work/tabTop.png" class="tabTop" alt="">
                                <img src="../../img/workflow/work/tabBot.png" class="tabBot" alt="">
                                <img src="../../img/workflow/work/tabDel.png" class="tabDel" alt="">
                            </td>
                        </tr>
                        </tbody>
                    </table>
                    <button id="addTabTwo">添加</button>
                </li>
                <li dataType="3" class="step">
                    <label style="position: relative;">
                        <div class="tit">统计方式</div>
                        <input type="radio" class="tjfs" name="fenzu" value="0" checked="true" dataType="0">
                        <span style="padding:0px 10px;">按分组统计计算</span>
                        <input type="radio" class="tjfs" name="fenzu" value="1" dataType="1">
                        <span style="padding-left: 10px;">按分组列出详情</span>
                    </label>
                    <div class="msg">分组统计可以对表单的某些字段进行统计计算并可以以图表方式体现；分组列出详情只是列出符合条件的工作记录详情</div>
                    <table>
                        <thead>
                        <tr>
                            <td style="width: 5%;">序号</td>
                            <td style="width: 140px;">选择表单字段</td>
                            <td style="width: 140px;">字段显示名称</td>
                            <td>计算方式</td>
                            <td style="width: 18%">操作</td>
                        </tr>
                        </thead>
                        <tbody class="list">
                        <tr>
                            <td class="num">1</td>
                            <td><select name="" class="chooseTab field" id="field">
                                <option value=""></option></select></td>
                            <td><input type="text" class="showName field"></td>
                            <td>
                                <select class="jisuan field" name="" >
                                    <option value="">无</option>
                                    <option value="0">求和</option>
                                    <option value="1">加权平均</option>
                                    <option value="2">求平均</option>
                                    <option value="3">自定义公式</option>
                                </select>
                                <span class="hidden qz">权重字段：<select name="" class="chooseTab qzz" id="qz"><option
                                        value=""></option></select></span>
                                <span class="hidden gs">公式：<input type="text" class="gs" id="gs"></span>
                            </td>
                            <td class="edit" >
                                <img src="../../img/workflow/work/tabTop.png" class="tabTop" alt="">
                                <img src="../../img/workflow/work/tabBot.png" class="tabBot" alt="">
                                <img src="../../img/workflow/work/tabDel.png" class="tabDel" alt="">
                            </td>
                        </tr>
                        </tbody>
                    </table>
                    <button id="addTab">添加</button>
                </li>
            </ul>
        </div>

    </div>
    <div class="con_bom">
        <button class="last disabled" dataType="1">上一步</button>
        <button class="nexts" dataType="1">下一步</button>
        <button class="save" dataType="1">保存</button>
        <button class="closes">关闭</button>
    </div>
    <script>
        var json = ''
        var rId = $.GetRequest().rId
        var arrTab =[];
        var arrTabs = [];
        if(rId == undefined){
            document.title='新建报表';
            $('.text').html('新建报表');
        }else{
            document.title='编辑报表';
            $('.text').html('编辑报表');
        }
        $(document).mouseup(function (e) {
            var con = $("#list");   // 设置目标区域
            var sel=$('.sel')
            if (!con.is(e.target) && !sel.is(e.target)  ) {
                $('.sel').hide()
            }
        })
//        $(document).click(function(e){
//            $('.sel').hide()
//        })
        $('#list').click(function(e){
            e.stopPropagation()
            if( $('.sel').css('display')!='none'){
                $('.sel').hide()
            }else{
                $('.sel').show()
            }

        })
        $('[name="flowName"]').keyup(function(){
            $('.sel').show()
            var val=$(this).val()
            $('.sel li').each(function(i,v){
//            console.log(v.innerHTML)
                if(v.innerHTML.indexOf(val)>-1){
                    $(v).show();
                    $(v).parent().show()
                }else{
                    $(v).hide();
                }
            })

        })

        $('.sel').on('click','li',function(e){
            e.stopPropagation()
            if($(this).attr('value')){
                $('[name="flowName"]').val($(this).html())
                $('[name="flowName"]').attr('dataType',$(this).attr('value'))
                $('.sel').hide()
                $.ajax({
                    url:'/reportSettings/querybyFlowIdField',
                    dataType:'json',
                    type:'get',
                    data:{flowId:$(this).attr('value')},
                    success:function(res){
                        var data = res.obj[0].printModel;
                        var str='';

                        var len = $(data).find('.form_item').length
                        for(var i = 0;i<len;i++){
                            str += '<option value="'+$(data).find('.form_item').eq(i).attr("name")+'">'+$(data).find('.form_item').eq(i).attr("title")+'</option>'
                        }
                        $('.chooseTab').append(str)
                        json = str;
                        $('[name="flowName"]').attr('formId',res.obj[0].formId)
                    }
                })
            }else{
                $('.sel').show()
            }
        })

        function buildNode(len,data){
            var prefix = 10;
            for(var i=0;i<len;i++){
                prefix += 10;
            }

            $.each(data,function(i,item){
                if(0 < item.childs.length){
                    $('.sel').append("<li style='padding-left:"+(prefix)+"px;font-weight:bold;font-size:14px;' id="+item.sortId +">" + item.sortName + "<li>");
                    $.each(item.flowTypeModels,function(j,v){
                        $('.sel').append("<li style='padding-left:"+(prefix+10)+"px;cursor:pointer' class='canchoose' value="+v.flowId +">" +  v.flowName + "<li>");
                    })
                    buildNode(len+1,item.childs);
                }else{
                    $('.sel').append("<li style='padding-left:"+(prefix)+"px;font-weight:bold;font-size:14px;' id="+item.sortId +">" + item.sortName + "<li>");
                    $.each(item.flowTypeModels,function(j,v){
                        $('.sel').append("<li style='padding-left:"+(prefix+10)+"px;cursor:pointer' class='canchoose' value="+v.flowId +">" +  v.flowName + "<li>");
                    })
                }
            });
        }

        $(function(){
            var index = layer.load();
//            获取表单流程
            $.ajax({
                url:"/flow/selOneToAllType",
                type:'post',
                dataType:'json',
                success:function(res){
                    var data=res.datas;
                    var str='<option value=""><fmt:message code="hr.th.PleaseSelect"/></option>';
                    if(res.flag){
                        $.each(data,function(i,item){
                            $('.sel').append("<li class='ones' style='font-weight:bold;font-size:14px;' id="+item.sortId +"><img src='../../img/data_points.png' style='margin-right: 5px;    margin-top: 0px;' alt=''>" + item.sortName + "<li>");
                            $.each(item.flowTypeModels,function(j,v){
                                $('.sel').append("<li style='padding-left:10px;cursor:pointer' class='canchoose' value="+v.flowId +">" +v.flowName + "<li>");
//                                $('[name="dispatchType"]').append("<option value="+v.flowId +">" + + "</option>");
                            })
                            buildNode(1,item.childs);
                        });
                    }
                    layer.close(index);
//                $('[name="flowId"]').html(str);
                }
            })

        })

//        点击下一步
        $('.nexts').click(function(){
            var dataType = Number($(this).attr('dataType')) ;
            if(dataType == 1){
                if($('[name="flowName"]').val()==""){
                    $.layerMsg({content:'请选择所属流程',icon:6});
                    return false;
                }
                if($('[name="tableName"]').val()==""){
                    $.layerMsg({content:'请填写报表名称',icon:6});
                    return false;
                }
            }
            if(dataType == 2){
                for(var i=0;i<$('.lists tr').length;i++){
                    if($('.lists tr').eq(i).find('.field').val() == ""){
                        alert('序号'+(i+1)+'的表单字段不能为空');
                        return false;
                    }
                }
                // for(var i=0;i<$('.list tr').length;i++){
                //     if($('.list tr').eq(i).find('#field').val() == ""){
                //         alert('序号'+(i+1)+'的表单字段不能为空');
                //         return false;
                //     }
                //     if($('.list tr').eq(i).find('.jisuan').val() == "" && !$('.list tr').eq(i).find('.jisuan').hasClass('jin')){
                //         alert('序号'+(i+1)+'的计算方式不能为空');
                //         return false;
                //     }
                // }
                $('.nexts').addClass('disabled');
            }
            if(dataType == 3){
                return false;
            }
            dataType ++;
            $('.outerbox li').eq(dataType-1).addClass('active').siblings().removeClass('active');
            $('.title').html($('.outerbox li').eq(dataType-1).html());
            var showType = dataType-1
            $('.content .step').eq(showType).show().siblings().hide();

            $('#tabName').html($('[name="tableName"]').val())
            $('.last').attr('dataType',dataType).removeClass('disabled')
            $('.nexts').attr('dataType',dataType)
            $('.save').attr('dataType',dataType)
        })

//        点击上一步
        $('.last').click(function(){
            var dataType = Number($(this).attr('dataType')) ;

            if(dataType == 2){
                $('.last').addClass('disabled');
            }
            if(dataType == 1){
                return false;
            }
            dataType --;
            $('.outerbox li').eq(dataType-1).addClass('active').siblings().removeClass('active');
            $('.title').html($('.outerbox li').eq(dataType-1).html());
            var showType = dataType-1
            $('.content .step').eq(showType).show().siblings().hide();
            $('#tabName').html($('[name="tableName"]').val())
            $('.last').attr('dataType',dataType)
            $('.nexts').attr('dataType',dataType).removeClass('disabled')
            $('.save').attr('dataType',dataType)
        })





//        切换统计方式
        $('.tjfs').click(function(){
            var dataType = $(this).attr('dataType');
            if(dataType ==0){
                $('.jisuan').prop('disabled',false).removeClass('jin');
                $('.qzz').prop('disabled',false).removeClass('jin');
                $('.gs').prop('disabled',false).removeClass('jin');
            }else{
                $('.jisuan').prop('disabled',true).addClass('jin');
                $('.qzz').val('')
                $('.gs').val('')
            }

        })

//        切换字段名称
        $(document).on('change','.chooseTab',function(){
            $(this).parent().next().find('.showName').val($(this).find('option:selected').text())
        })

//        计算公式切换
        $(document).on('change','.jisuan',function(){
            var val = $(this).find('option:selected').val();
            if(val == 1){
                $(this).parent().find('.qz').show();
                $(this).parent().find('.gs').hide();
            }else if(val == 3){
                $(this).parent().find('.gs').show();
                $(this).parent().find('.qz').hide();

            }else{
                $(this).parent().find('.gs').hide();
                $(this).parent().find('.qz').hide();

            }
        })

//        点击添加统计方式table
        $('#addTab').click(function(){
            var len =  $('.list tr').length;
            if($('[name="fenzu"]:checked').val() == 0){
                var tr = '<tr>\n' +
                    '                            <td class="num">'+(len+1)+'</td>\n' +
                    '                            <td><select name="" class="chooseTab field" id="field">\n' +
                    '                                <option value=""></option>'+json+'</select></td>\n' +
                    '                            <td><input type="text" class="showName field"></td>\n' +
                    '                            <td>\n' +
                    '                                <select class="jisuan field" name="" >\n' +
                    '                                    <option value="">无</option>\n' +
                    '                                    <option value="0">求和</option>\n' +
                    '                                    <option value="1">加权平均</option>\n' +
                    '                                    <option value="2">求平均</option>\n' +
                    '                                    <option value="3">自定义公式</option>\n' +
                    '                                </select>\n' +
                    '                                <span class="hidden qz">权重字段：<select name="" class="chooseTab qzz" id="qz"><option\n' +
                    '                                        value=""></option>'+json+'</select></span>\n' +
                    '                                <span class="hidden gs">公式：<input type="text" class="gs" id="gs"></span>\n' +
                    '                            </td>\n' +
                    '                            <td class="edit">\n' +
                    '                                <img src="../../img/workflow/work/tabTop.png" class="tabTop" alt="">\n' +
                    '                                <img src="../../img/workflow/work/tabBot.png" class="tabBot" alt="">\n' +
                    '                                <img src="../../img/workflow/work/tabDel.png" class="tabDel" alt="">\n' +
                    '                            </td>\n' +
                    '                        </tr>';
            }else{
                var tr = '<tr>\n' +
                    '                            <td class="num">'+(len+1)+'</td>\n' +
                    '                            <td><select name="" class="chooseTab field" id="field">\n' +
                    '                                <option value=""></option>'+json+'</select></td>\n' +
                    '                            <td><input type="text" class="showName field"></td>\n' +
                    '                            <td>\n' +
                    '                                <select class="jisuan field jin" name="" disabled="disabled">\n' +
                    '                                    <option value="">无</option>\n' +
                    '                                    <option value="0">求和</option>\n' +
                    '                                    <option value="1">加权平均</option>\n' +
                    '                                    <option value="2">求平均</option>\n' +
                    '                                    <option value="3">自定义公式</option>\n' +
                    '                                </select>\n' +
                    '                                <span class="hidden qz">权重字段：<select name="" class="chooseTab qzz" id="qz"><option\n' +
                    '                                        value=""></option>'+json+'</select></span>\n' +
                    '                                <span class="hidden gs">公式：<input type="text" class="gs" id="gs"></span>\n' +
                    '                            </td>\n' +
                    '                            <td class="edit">\n' +
                    '                                <img src="../../img/workflow/work/tabTop.png" class="tabTop" alt="">\n' +
                    '                                <img src="../../img/workflow/work/tabBot.png" class="tabBot" alt="">\n' +
                    '                                <img src="../../img/workflow/work/tabDel.png" class="tabDel" alt="">\n' +
                    '                            </td>\n' +
                    '                        </tr>';
            }


            $('.list').append(tr)
        })
//        添加查询字段table
        $('#addTabTwo').click(function(){
            var len =  $('.lists tr').length;
            var trs = ' <tr>\n' +
                '                            <td class="num">'+(len+1)+'</td>\n' +
                '                            <td><select name="" class="chooseTab field">\n' +
                '                                <option value=""></option>'+json+'</select>' +
                '                         </td>\n' +
                '                            <td><input class="showName field" type="text"></td>\n' +
                '                            <td class="edits">\n' +
                '                                <img src="../../img/workflow/work/tabTop.png" class="tabTop" alt="">\n' +
                '                                <img src="../../img/workflow/work/tabBot.png" class="tabBot" alt="">\n' +
                '                                <img src="../../img/workflow/work/tabDel.png" class="tabDel" alt="">\n' +
                '                            </td>\n' +
                '                        </tr>';
            $('.lists').append(trs);

        })

//        删除
        $('.list').on('click','.tabDel',function(){
            $(this).parents('tr').remove();
            var len = $('.list tr').length;
            for(var i=0;i<len;i++){
                $('.list .num').eq(i).html((i+1))
            }
        })
        $('.lists').on('click','.tabDel',function(){
            $(this).parents('tr').remove();
            var len = $('.lists tr').length;
            for(var i=0;i<len;i++){
                $('.lists .num').eq(i).html((i+1))
            }
        })
//        上移
        $('.list').on('click','.tabTop',function(){
            if($(this).parents('tr').prev()){
                $(this).parents('tr').prev().before($(this).parents('tr'))
                var len = $('.list tr').length;
                for(var i=0;i<len;i++){
                    $('.list .num').eq(i).html((i+1))
                }
            }
        })
        $('.lists').on('click','.tabTop',function(){
            if($(this).parents('tr').prev()){
                $(this).parents('tr').prev().before($(this).parents('tr'))
                var len = $('.lists tr').length;
                for(var i=0;i<len;i++){
                    $('.list .num').eq(i).html((i+1))
                }
            }
        })
//        下移
        $('.list').on('click','.tabBot',function(){
            if($(this).parents('tr').next()){
                $(this).parents('tr').next().after($(this).parents('tr'))
                var len = $('.list tr').length;
                for(var i=0;i<len;i++){
                    $('.list .num').eq(i).html((i+1))
                }
            }
        })
        $('.lists').on('click','.tabBot',function(){
            if($(this).parents('tr').next()){
                $(this).parents('tr').next().after($(this).parents('tr'))
                var len = $('.lists tr').length;
                for(var i=0;i<len;i++){
                    $('.list .num').eq(i).html((i+1))
                }
            }
        })
        function getZD(id,name,val,fn){
            $.ajax({
                url:'/reportSettings/querybyFlowIdField',
                dataType:'json',
                type:'get',
                data:{flowId:id},
                success:function(res){
                    var data = res.obj[0].printModel;
                    var str='';

                    var len = $(data).find('.form_item').length
                    for(var i = 0;i<len;i++){
                        str += '<option value="'+$(data).find('.form_item').eq(i).attr("name")+'">'+$(data).find('.form_item').eq(i).attr("title")+'</option>'
                    }

                    $('.chooseTab').append(str);
                    json = str;
                    $(name).find('option[value="'+val+'"]').prop('selected','selected')
                    if(fn){
                        fn()
                    }
                }
            })
        }

//        编辑
        if(rId != undefined){
            $.ajax({
                url:'/reportSettings/queryIdReportSettings',
                dataType:'json',
                type:'get',
                data:{rId:rId},
                success:function(res){
                    var data=res.obj[0]
                    $('[name="flowName"]').val(data.flowName)
                    $('[name="flowName"]').attr('dataType',data.flowId);
                    $('[name="tableName"]').val(data.rName);
                    $('[name="fenzu"][value="'+data.groupType+'"]').prop('checked',true);

                    $('#tabName').html(data.rName)
                    getZD(data.flowId,'#fenzu',data.groupField,function(){
//                        统计方式table回显
                        var arr1 = data.listItem.split('|');
                        var str = "";
                        for(var i=0;i<arr1.length;i++){
                            if(arr1[i]!="") {
                                var array = []
                                var arrs = arr1[i].split(',')
                                for (var j = 0; j < arrs.length; j++) {
                                    var arrss = arrs[j].split(':');
                                    array.push(arrss[1])
                                }
                                arrTab.push(array)
                                if (array[0] != "run_name" && array[0] != "run_id") {
                                    var tab = '<tr>' +
                                        '<td class="num">' + (i + 1) + '</td>' +
                                        '<td><select name="" class="chooseTab field" id="field">' +
                                        '<option value=""></option>' + json + '</select></td>' +
                                        '<td><input type="text" class="showName field" value="' + array[3] + '"></td>' +
                                        '<td>' +
                                        '<select class="jisuan field" name="" >' +
                                        '<option value="">无公式</option>' +
                                        '<option value="0">求和</option>' +
                                        '<option value="1">加权平均</option>' +
                                        '<option value="2">求平均</option>' +
                                        '<option value="3">自定义公式</option>' +
                                        '</select>' +
                                        '<span class="hidden qz">权重字段：<select name="" class="chooseTab qzz" id="qz"><option value=""></option>' + json + '</select></span>' +
                                        '<span class="hidden gs">公式：<input type="text" class="gs" id="gs"></span>' +
                                        '</td>' +
                                        '<td class="edit">' +
                                        '<img src="../../img/workflow/work/tabTop.png" class="tabTop" alt=""  style="margin-left: -12px">' +
                                        '<img src="../../img/workflow/work/tabBot.png" class="tabBot" alt="">' +
                                        '<img src="../../img/workflow/work/tabDel.png" class="tabDel" alt="">' +
                                        '</td>' +
                                        '</tr>';
                                    str += tab;
                                }
                            }
                        }
                        $('.list').html(str)
                        for(var i=0;i<$('.list tr').length;i++){
                            var tdd = $('.list tr').eq(i)
                            tdd.find('#field').val($.trim(arrTab[i][0]))
                            if($.trim(arrTab[i][1])==0){
                                tdd.find('.jisuan').val('0')
                                tdd.find('.qz').hide()
                                tdd.find('.gs').hide()
                            }else if($.trim(arrTab[i][1])==1){
                                tdd.find('.jisuan').val('1')
                                tdd.find('.qz').removeClass('hidden')
                                tdd.find('#qz').val($.trim(arrTab[i][4]))
                                tdd.find('.gs').hide()
                            }else if($.trim(arrTab[i][1])==2){
                                tdd.find('.jisuan').val('2')
                                tdd.find('.qz').hide()
                                tdd.find('.gs').hide()
                            }else if($.trim(arrTab[i][1])==3){
                                tdd.find('.jisuan').val('3')
                                tdd.find('.qz').hide()
                                tdd.find('.gs').removeClass('hidden')
                                tdd.find('#gs').val($.trim(arrTab[i][2]))
                            }
                        }

//                        查询字段table回显
                        var seaArr = data.queryItem.split('|');
                        var strTwo = "";
                        for(var i=0;i<seaArr.length;i++){
                            if(seaArr[i]!=""){
                                var arrays=[]
                                var arrTwo = seaArr[i].split(',')
                                for(var j=0;j<arrTwo.length;j++){
                                    var arrTwos = arrTwo[j].split(':');
                                    arrays.push(arrTwos[1])
                                }
                                arrTabs.push(arrays)
//                                console.log(arrTab)
                                var tabs=  ' <tr>\n' +
                                    '                            <td class="num">'+(i+1)+'</td>\n' +
                                    '                            <td><select name="" id="zd" class="chooseTab field">\n' +
                                    '                                <option value=""></option>'+json+'</select>' +
                                    '                         </td>\n' +
                                    '                            <td><input class="showName field" type="text" value="'+arrays[1]+'"></td>\n' +
                                    '                            <td class="edits">\n' +
                                    '                                <img src="../../img/workflow/work/tabTop.png" class="tabTop" alt="">\n' +
                                    '                                <img src="../../img/workflow/work/tabBot.png" class="tabBot" alt="">\n' +
                                    '                                <img src="../../img/workflow/work/tabDel.png" class="tabDel" alt="">\n' +
                                    '                            </td>\n' +
                                    '                        </tr>';
                                strTwo += tabs;
                                }
                        }
                        $('.lists').html(strTwo)
                        for(var i=0;i<$('.lists tr').length;i++){
                            var tdds = $('.lists tr').eq(i)
                            tdds.find('#zd').val($.trim(arrTabs[i][0]))
                            tdds.find('.showName').val($.trim(arrTabs[i][1]))

                        }

                        if(data.groupType == 1){
                            $('.jisuan').prop('disabled',true).addClass('jin');
                            $('.qzz').val('')
                            $('.gs').val('')
                        }else{
                            $('.jisuan').prop('disabled',false).removeClass('jin');
                            $('.qzz').prop('disabled',false).removeClass('jin');
                            $('.gs').prop('disabled',false).removeClass('jin');
                        }


                    });





                }
            })
        }
//关闭
        $('.closes').click(function(){
            window.close();
        })

//      保存
        $('.save').click(function(){
            if($('[name="flowName"]').val()==""){
                $.layerMsg({content:'请选择所属流程',icon:6});
                return false;
            }
            if($('[name="tableName"]').val()==""){
                $.layerMsg({content:'请填写报表名称',icon:6});
                return false;
            }

            for(var i=0;i<$('.lists tr').length;i++){
                if($('.lists tr').eq(i).find('.field').val() == ""){
                    alert('序号'+(i+1)+'的表单字段不能为空');
                    return false;
                }
            }
            for(var i=0;i<$('.list tr').length;i++){
                if($('.list tr').eq(i).find('#field').val() == ""){
                    alert('序号'+(i+1)+'的表单字段不能为空');
                    return false;
                }
               /* if($('.list tr').eq(i).find('.jisuan').val() == "" && !$('.list tr').eq(i).find('.jisuan').hasClass('jin')){
                    alert('序号'+(i+1)+'的计算方式不能为空');
                    return false;
                }*/
            }

            var arr1=[];
            for(var i =0;i<$('.list tr').length;i++){
                var td = $('.list tr').eq(i).find('td')
                var field = td.find('.field').eq(0).val();
                var dispName = td.find('.field').eq(1).val();
                var calcType = td.find('.field').eq(2).val();
                if(calcType == 1){
                    var weight = td.find('#qz').val();
                    var formulate =""
                }else if(calcType == 3){
                    var weight = ""
                    var formulate =td.find('#gs').val();
                }else{
                    var weight = ""
                    var formulate =""
                }
                var data="field="+field+",disp_name="+dispName+",calc_type="+calcType+",formulate="+formulate+",wieght="+weight;
                arr1.push(data)
            }
            arr1.push("field=run_id,disp_name=流水号,calc_type='',formulate='',wieght=''");
            arr1.push("field=run_name,disp_name=文号,calc_type='',formulate='',wieght=''");
            var arr2=[];
            for(var i =0;i<$('.lists tr').length;i++){
                var td = $('.lists tr').eq(i).find('td')
                var field = td.find('.field').eq(0).val();
                var dispName = td.find('.field').eq(1).val();

                var data="field="+field+",disp_name="+dispName;
                arr2.push(data)
            }
            if(arr1.length == 1){
                arr1.push("")
            }
            if(arr2.length == 1){
                arr2.push("")
            }
            var data={
                flowId:$('[name="flowName"]').attr('dataType'),
                rName:$('[name="tableName"]').val(),
                formId:$('[name="flowName"]').attr('formId'),
                groupField:$('#fenzu').val(),
                groupType:$('[name="fenzu"]:checked').val(),
                listItem:arr1,
                queryItem:arr2
            };

            if(rId == undefined){
                var  r=confirm("确认保存此工作？");
                if(r){
                    $.ajax({
                        url:'/reportSettings/newReportSettings',
                        type:'get',
                        dataType:'json',
                        data:data,
                        success:function(res){
                            if(res.flag){
                                $.layerMsg({content:'保存成功',icon:1})
                                window.opener.location.href=window.opener.location.href;
                                // parent.location.reload();
                                window.close();
                            }else{
                                $.layerMsg({content:'保存失败',icon:2})
                            }
                        }
                    })
                }
            }else{
                var  r=confirm("确认保存此工作？");
                if(r){
                    data.rId = rId;
                    $.ajax({
                        url:'/reportSettings/updateReportSettings',
                        type:'get',
                        dataType:'json',
                        data:data,
                        success:function(res){
                            if(res.flag){
                                $.layerMsg({content:'保存成功',icon:1})
                                window.opener.location.href=window.opener.location.href;
                                window.close();
                            }else{
                                $.layerMsg({content:'保存失败',icon:2})
                            }
                        }
                    })
                }

            }

        })


    </script>
</body>
</html>
